package com.example.api.controller;


import com.example.api.entity.Card;
import com.example.api.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cards")
public class CardController {
    @Autowired
    private CardService cardService;



//    获取所有轮播图
    @GetMapping
    public List<Card> getAllCards() {
        return cardService.getAllCards();
    }




    // 添加卡片
    @PostMapping
    public ResponseEntity<Card> addCard(@RequestBody Card card) {
        cardService.addCard(card);
        cardService.updateAllCards();
        return new ResponseEntity<>(card, HttpStatus.CREATED); // 返回卡片和 201 Created 状态
    }

    // 删除卡片
    @DeleteMapping("/{cardId}")
    public ResponseEntity<Void> deleteCard(@PathVariable int cardId) {
        cardService.deleteCard(cardId);
        return ResponseEntity.noContent().build();  // 返回 204 No Content 状态
    }

    // 更新卡片
    @PutMapping("/{cardId}")
    public ResponseEntity<Card> updateCard(@PathVariable int cardId, @RequestBody Card card) {
        card.setCardId(cardId); // 确保请求体中的 cardId 和路径中的一致
        cardService.updateCard(card);
        return new ResponseEntity<>(card, HttpStatus.OK);  // 返回更新后的卡片和 200 OK 状态
    }

    // 获取卡片
    @GetMapping("/{cardId}")
    public ResponseEntity<Card> getCard(@PathVariable int cardId) {
        Card card = cardService.getCardById(cardId);  // 需要在 CardService 中实现获取卡片的方法
        if (card != null) {
            return new ResponseEntity<>(card, HttpStatus.OK);  // 返回卡片和 200 OK 状态
        } else {
            return ResponseEntity.notFound().build();  // 返回 404 Not Found 状态
        }
    }

}